package tt.com.warehousemanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.AuditEnum;
import tt.com.common.enums.Status;
import tt.com.common.model.YxCommonPurchase;
import tt.com.common.model.YxCommonPurchaseServe;
import tt.com.common.util.*;
import tt.com.warehousemanager.service.PurchaseServeService;
import tt.com.warehousemanager.service.PurchaseService;
import tt.com.warehousemanager.vo.AuditVo;
import tt.com.warehousemanager.vo.PurchaseVo;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * @author Czycka
 * @date 2022/8/19 23:14
 * @description 采购信息控制类
 */
@RestController
@RequestMapping(value = "warehouse/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private PurchaseServeService serveService;

    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody PurchaseVo purchaseVo) {
        BigDecimal totalCost = new BigDecimal(0);
        String purchaseNo = UUID.randomUUID().toString();
        for (YxCommonPurchaseServe item : purchaseVo.getPurchaseServes()) {
            if (!ObjectUtil.objectIsNull(item)) {
                return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
            }
            // 计算小计金额 数量 * 单价
            item.setCost(item.getCount().multiply(item.getPrice()));
            // 累加总金额
            totalCost = totalCost.add(item.getCost());
            // 设置明细内的采购订单号
            item.setOrganId(purchaseNo);
        }
        YxCommonPurchase purchase = new YxCommonPurchase();
        // 设置总金额
        purchase.setTotalCost(totalCost);
        // 设置采购时间
        purchase.setPurchaseTime(purchaseVo.getPurchaseTime());
        // 设置采购状态
        purchase.setStatus(AuditEnum.PENDING_REVIEW);
        // 设置采购订单号
        purchase.setPurchaseNo(purchaseNo);

        if (purchaseService.insertPurchase(purchase, "2") < 0) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "采购单生成异常");
        }
        if (!serveService.saveBatch(purchaseVo.getPurchaseServes())) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "采购单明细生成异常");
        }
        return CommonResult.buildR(Status.OK, "采购单生成成功");
    }

    @PostMapping(value = "delete/{purchaseNo}")
    public CommonResult delete(@PathVariable("purchaseNo") Long purchaseNo) {
        if (StringUtils.isEmpty(purchaseNo)) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        QueryWrapper<YxCommonPurchase> wrapper = new QueryWrapper<>();
        wrapper.eq("id", purchaseNo);
        YxCommonPurchase one = purchaseService.getOne(wrapper);
        // 订单表 或订单明细表 哪个删除失败都返回失败
        if (purchaseService.deletePurchase(purchaseNo, "2") < 0 || !serveService.deletePurchaseServe(one.getPurchaseNo(), "2")) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "采购单删除异常");
        } else {
            return CommonResult.buildR(Status.OK, "采购单删除成功");
        }
    }

    @PostMapping(value = "update")
    public CommonResult update(@RequestBody YxCommonPurchase purchase) {
        if (!ObjectUtil.objectIsNull(purchase)) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if (purchaseService.updatePurchase(purchase, "2") > 0) {
            return CommonResult.buildR(Status.OK, "采购单日期修改成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "采购单日期修改失败");
        }
    }

    @PostMapping(value = "update/record")
    public CommonResult updateRecord(@RequestBody List<YxCommonPurchaseServe> purchaseServes) {
        BigDecimal totalCost = new BigDecimal(0);
        String purchaseNo = null;
        for (YxCommonPurchaseServe item : purchaseServes) {
            if (!ObjectUtil.objectIsNull(item)) {
                return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
            }
            // 计算小计金额 数量 * 单价
            item.setCost(item.getCount().multiply(item.getPrice()));
            // 累加总金额
            totalCost = totalCost.add(item.getCost());
            // 设置明细内的采购订单号
            purchaseNo = item.getOrganId();
        }
        QueryWrapper<YxCommonPurchase> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_no", purchaseNo);
        YxCommonPurchase purchase = purchaseService.getOne(wrapper);
        purchase.setTotalCost(totalCost);
        if (purchaseService.updatePurchase(purchase, "2") < 0) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "采购单修改异常");
        } else if (!serveService.updateBatchById(purchaseServes)) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "采购明细修改异常");
        } else {
            return CommonResult.buildR(Status.OK, "采购信息修改成功");
        }
    }

    @PostMapping(value = "show/page")
    public CommonResult showPage(@RequestBody PageUtil pageUtil) {
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        pageUtil = purchaseService.showPurchaseByPage(pageUtil);
        return CommonResult.buildR(Status.OK, "查询成功", pageUtil);
    }

    @PostMapping(value = "show/all")
    public CommonResult showAll() {
        return CommonResult.buildR(Status.OK, "查询成功", purchaseService.list());
    }

    @PostMapping(value = "show/{id}")
    public CommonResult showPrint(@PathVariable String id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        return CommonResult.buildR(Status.OK, "查询成功", purchaseService.getById(id));
    }

    @PostMapping(value = "show/record/{no}")
    public CommonResult showRecord(@PathVariable("no") String no) {
        if (no == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        QueryWrapper<YxCommonPurchaseServe> wrapper = new QueryWrapper<>();
        wrapper.eq("organ_id", no);
        List<YxCommonPurchaseServe> list = serveService.list(wrapper);
        System.out.println(list.size());
        return list.size() == 0 ? CommonResult.buildR(Status.OK, "数据为空") : CommonResult.buildR(Status.OK, "查询成功", list);
    }

    @PostMapping(value = "audit")
    public CommonResult audit(@RequestBody AuditVo auditVo) {
        // 使用枚举工具类返回美剧
        AuditEnum status = EnumUtil.getEnumByCode(auditVo.getAuditType(), AuditEnum.class);
        // 拼接成审核工具类
        AuditUtil auditUtil = new AuditUtil("2", auditVo.getAuditId(), status, auditVo.getCause());
        if (purchaseService.auditing(auditUtil) > 0) {
            return CommonResult.buildR(Status.OK, "采购单审核成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "采购单审核失败");
        }

    }
}
